package com.greyu.ysj.service.impl;



import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.github.pagehelper.PageHelper;
import com.greyu.ysj.config.ResultStatus;
import com.greyu.ysj.entity.CbtChange;
import com.greyu.ysj.entity.CbtChangeExample;
import com.greyu.ysj.mapper.CbtChangeMapper;
import com.greyu.ysj.model.ResultModel;
import com.greyu.ysj.service.CbtChangeService;

@Service
@Transactional
public class CbtChangeServiceImpl implements CbtChangeService {
	@Autowired
	private CbtChangeMapper cbtChangeMapper;

	@Override
	public ResultModel updateChange(int Id, CbtChange cbtChange) {
		CbtChange newChange= this.cbtChangeMapper.selectByPrimaryKey(Id);
		if(newChange == null) {
			return ResultModel.error(ResultStatus.DATA_NOT_FOUND);
        }
		if(cbtChange.getStatus() != null) {
			newChange.setStatus(cbtChange.getStatus());
		}
		newChange.setUpdateDate(new Date());
		this.cbtChangeMapper.updateByPrimaryKey(newChange);
        return ResultModel.ok(newChange);
	}

	@Override
	public ResultModel save(CbtChange cbtChange) {
		if (cbtChange.getUserId() == null || cbtChange.getCoinId() == null
				|| cbtChange.getAddress() == null || cbtChange.getAmount() == null) {
            return ResultModel.error(ResultStatus.DATA_NOT_NULL);
        }
		cbtChange.setStatus(-1);
		cbtChange.setCreateDate(new Date());
		this.cbtChangeMapper.insert(cbtChange);
		System.out.println("change插入成功");
		return ResultModel.ok(cbtChange);
	}

	@Override
	public List<CbtChange> getChangeRecord(Integer userId, Integer coinId,Integer page, Integer rows) {
		if (null != page && null != rows) {
            PageHelper.startPage(page, rows);
        }
		CbtChangeExample cbtChangeExample = new CbtChangeExample();
		CbtChangeExample.Criteria criteria =cbtChangeExample.createCriteria();
		criteria.andUserIdEqualTo(userId);
		criteria.andCoinIdEqualTo(coinId);
		cbtChangeExample.setOrderByClause("`id` DESC");
		List<CbtChange>list=this.cbtChangeMapper.selectByExample(cbtChangeExample);
		return list;
	}

	@Override
	public int totalPages(Integer userId, Integer coinId, Integer rows) {
		CbtChangeExample cbtChangeExample = new CbtChangeExample();
		CbtChangeExample.Criteria criteria =cbtChangeExample.createCriteria();
		criteria.andUserIdEqualTo(userId);
		criteria.andCoinIdEqualTo(coinId);
		long total=this.cbtChangeMapper.countByExample(cbtChangeExample);
		int totalPages;
		if(total%rows == 0) {
			totalPages=(int) (total/rows);
		}else {
			totalPages=(int) (total/rows)+1;
		}
		System.out.println("totalPages_change=>"+totalPages);
		return totalPages;
	}

	@Override
	public CbtChange selectById(Integer Id) {
		CbtChange cbtChange=this.cbtChangeMapper.selectByPrimaryKey(Id);
		return cbtChange;
	}

	@Override
	public List<CbtChange> getChangeList(Integer status, Integer userId, String coinSymbol, Integer page,
			Integer rows) {
		if (null != page && null != rows) {
            PageHelper.startPage(page, rows);
        }
		CbtChangeExample cbtChangeExample = new CbtChangeExample();
		CbtChangeExample.Criteria criteria =cbtChangeExample.createCriteria();
		if(status != null) {
			criteria.andStatusEqualTo(status);
		}
		if(userId != null) {
			criteria.andUserIdEqualTo(userId);
		}
		if(coinSymbol != null && !coinSymbol.equals("")) {
			criteria.andCoinSymbolEqualTo(coinSymbol);
		}
		cbtChangeExample.setOrderByClause("`id` ASC");
		List<CbtChange>list=this.cbtChangeMapper.selectByExample(cbtChangeExample);
		return list;
	}

	@Override
	public int getTotalPages(Integer status, Integer userId, String coinSymbol, Integer rows) {
		CbtChangeExample cbtChangeExample = new CbtChangeExample();
		CbtChangeExample.Criteria criteria =cbtChangeExample.createCriteria();
		if(status != null) {
			criteria.andStatusEqualTo(status);
		}
		if(userId != null) {
			criteria.andUserIdEqualTo(userId);
		}
		if(coinSymbol != null && !coinSymbol.equals("")) {
			criteria.andCoinSymbolEqualTo(coinSymbol);
		}
		long total=this.cbtChangeMapper.countByExample(cbtChangeExample);
		int totalPages;
		if(total%rows==0) {
			totalPages=(int) (total/rows);
		}else {
			totalPages=(int) (total/rows)+1;
		}
		return totalPages;
	}

}
